using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.DataTier.Compiled;

namespace OR.Lazarus.Library.DataTier.Compiled.Lazarus
{
	/// <summary>
	/// Defines a class, with fields and methods.
	/// </summary>
   public class TableDetail : EntityBase
   {
		protected System.String mName;
		protected string mAlias;
		protected string mType;
		protected string mFilename;
		protected string mNamespace;
		protected string mFriendlyName;
		protected string mLogicalName;
		protected string mFilegroup;
		protected string mDescription;
	
		protected Derived.FieldCollection mFields;
		protected Derived.MethodCollection mMethods;
		protected Derived.ViewCollection mViews;
		protected Derived.ImplementCollection mImplements;
		protected Derived.PermissionCollection mPermissions;
		protected Derived.TableRefCollection mAllFields;

		public TableDetail()
		{
			mFields = new Derived.FieldCollection();
			mFields.ParentEntity = this;
			mMethods = new Derived.MethodCollection();
			mMethods.ParentEntity = this;
			mViews = new Derived.ViewCollection();
			mViews.ParentEntity = this;
			mImplements = new Derived.ImplementCollection();
			mImplements.ParentEntity = this;
			mPermissions = new Derived.PermissionCollection();
			mPermissions.ParentEntity = this;
			mAllFields = new Derived.TableRefCollection();
			mAllFields.ParentEntity = this;
	
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Alias
		{
			get
			{
				return ( mAlias );
			}
			set
			{
				mAlias = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Type
		{
			get
			{
				return ( mType );
			}
			set
			{
				mType = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Filename
		{
			get
			{
				return ( mFilename );
			}
			set
			{
				mFilename = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Namespace
		{
			get
			{
				return ( mNamespace );
			}
			set
			{
				mNamespace = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string FriendlyName
		{
			get
			{
				return ( mFriendlyName );
			}
			set
			{
				mFriendlyName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string LogicalName
		{
			get
			{
				return ( mLogicalName );
			}
			set
			{
				mLogicalName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Filegroup
		{
			get
			{
				return ( mFilegroup );
			}
			set
			{
				mFilegroup = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.FieldCollection Fields
		{
			get
			{
				return ( mFields );
			}
			set
			{
				mFields = value;
			}
		}

		public Derived.MethodCollection Methods
		{
			get
			{
				return ( mMethods );
			}
			set
			{
				mMethods = value;
			}
		}

		public Derived.ViewCollection Views
		{
			get
			{
				return ( mViews );
			}
			set
			{
				mViews = value;
			}
		}

		public Derived.ImplementCollection Implements
		{
			get
			{
				return ( mImplements );
			}
			set
			{
				mImplements = value;
			}
		}

		public Derived.PermissionCollection Permissions
		{
			get
			{
				return ( mPermissions );
			}
			set
			{
				mPermissions = value;
			}
		}

		public Derived.TableRefCollection AllFields
		{
			get
			{
				return ( mAllFields );
			}
			set
			{
				mAllFields = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mAlias = DataUtility.GetString( pSource, "alias" );
			mType = DataUtility.GetString( pSource, "type" );
			mFilename = DataUtility.GetString( pSource, "filename" );
			mNamespace = DataUtility.GetString( pSource, "namespace" );
			mFriendlyName = DataUtility.GetString( pSource, "friendly-name" );
			mLogicalName = DataUtility.GetString( pSource, "logical-name" );
			mFilegroup = DataUtility.GetString( pSource, "filegroup" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lzc:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vFields = ( XmlElement )pSource.SelectSingleNode( "lzc:fields", PersistenceConfig.Namespaces );
			
			if ( vFields != null )
			{
				mFields.LoadXml( vFields );
			}
			XmlElement vMethods = ( XmlElement )pSource.SelectSingleNode( "lzc:methods", PersistenceConfig.Namespaces );
			
			if ( vMethods != null )
			{
				mMethods.LoadXml( vMethods );
			}
			XmlElement vViews = ( XmlElement )pSource.SelectSingleNode( "lzc:views", PersistenceConfig.Namespaces );
			
			if ( vViews != null )
			{
				mViews.LoadXml( vViews );
			}
			XmlElement vImplements = ( XmlElement )pSource.SelectSingleNode( "lzc:implements", PersistenceConfig.Namespaces );
			
			if ( vImplements != null )
			{
				mImplements.LoadXml( vImplements );
			}
			XmlElement vPermissions = ( XmlElement )pSource.SelectSingleNode( "lzc:permissions", PersistenceConfig.Namespaces );
			
			if ( vPermissions != null )
			{
				mPermissions.LoadXml( vPermissions );
			}
			XmlElement vAllFields = ( XmlElement )pSource.SelectSingleNode( "lzc:all-fields", PersistenceConfig.Namespaces );
			
			if ( vAllFields != null )
			{
				mAllFields.LoadXml( vAllFields );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lzc", "table", "http://schemas.icitadel.com/Lazarus/Compiled.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mAlias ) )
			{
				vNewElement.SetAttribute( "alias", mAlias );
			}
					
			if ( !String.IsNullOrEmpty( mType ) )
			{
				vNewElement.SetAttribute( "type", mType );
			}
					
			if ( !String.IsNullOrEmpty( mFilename ) )
			{
				vNewElement.SetAttribute( "filename", mFilename );
			}
					
			if ( !String.IsNullOrEmpty( mNamespace ) )
			{
				vNewElement.SetAttribute( "namespace", mNamespace );
			}
					
			if ( !String.IsNullOrEmpty( mFriendlyName ) )
			{
				vNewElement.SetAttribute( "friendly-name", mFriendlyName );
			}
					
			if ( !String.IsNullOrEmpty( mLogicalName ) )
			{
				vNewElement.SetAttribute( "logical-name", mLogicalName );
			}
					
			if ( !String.IsNullOrEmpty( mFilegroup ) )
			{
				vNewElement.SetAttribute( "filegroup", mFilegroup );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lzc", "description", "http://schemas.icitadel.com/Lazarus/Compiled.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mFields.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mMethods.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mViews.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mImplements.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mPermissions.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mAllFields.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.TableDetail v_oReturn = new Derived.TableDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Alias = mAlias;
			v_oReturn.Type = mType;
			v_oReturn.Filename = mFilename;
			v_oReturn.Namespace = mNamespace;
			v_oReturn.FriendlyName = mFriendlyName;
			v_oReturn.LogicalName = mLogicalName;
			v_oReturn.Filegroup = mFilegroup;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.FieldDetail vDetail in mFields )
			{
				v_oReturn.Fields.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.MethodDetail vDetail in mMethods )
			{
				v_oReturn.Methods.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.ViewDetail vDetail in mViews )
			{
				v_oReturn.Views.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.ImplementDetail vDetail in mImplements )
			{
				v_oReturn.Implements.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.PermissionDetail vDetail in mPermissions )
			{
				v_oReturn.Permissions.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.TableRefDetail vDetail in mAllFields )
			{
				v_oReturn.AllFields.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.TableDetail CloneTyped()
		{
			return ( ( Derived.TableDetail )this.Clone() );
		}
   }
}
   